package com.feonix.complat.dao;

import org.springframework.stereotype.Repository;

import com.feonix.common.basedao.BaseJdbcDAO;
import com.feonix.common.basedao.Query;
import com.feonix.complat.constant.Tables;
import com.feonix.complat.entity.AccessLog;

/**
 * 登录日志
 * 
 * @author feonix
 * 
 */
@Repository
public class AccessLogDAO extends BaseJdbcDAO<Integer, AccessLog> {

	/**
	 * 查询登录日志数量
	 * 
	 * @param loginName
	 * @return
	 */
	public int findCount(Integer userid) {
		String sql = "SELECT COUNT(*) FROM " + Tables.ACCESSLOG + " WHERE userid = :userId";
		Query query = createQuery(sql);
		query.addParameter("userId", userid);
		int count = this.queryForInteger(query);
		return count;
	}

	/**
	 * 删除id最小的记录
	 * 
	 * @param userId
	 * @param deleteNumber
	 */
	public void deleteByMinId(Integer userId) {
		String sql = "DELETE FROM " + Tables.ACCESSLOG
				+ " WHERE iid = (SELECT c.minid FROM (SELECT MIN(b.iid) minid FROM "
				+ Tables.ACCESSLOG + " b WHERE b.loginname = :userId) c)";
		Query query = createQuery(sql);
		query.addParameter("userId", userId);
		this.delete(query);
	}
}
